package com.biz.primus.ms.promotion.dao.redis;

import com.biz.primus.base.redis.CrudRedisDao;
import com.biz.primus.ms.promotion.dao.ro.ActivityItemRo;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

/**
 * 活动项Redis Dao
 *
 * @author david-liu
 */
@Repository
public class ActivityItemRedisDao extends CrudRedisDao<ActivityItemRo, Long> {

    public List<ActivityItemRo> getActivityItems(Long activityId) {
        String activityMappedActivityItemsSortedSetKey = this.getActivityMappedActivityItemsSortedSetKey(activityId);
        Set<byte[]> bytes = super.zRange(activityMappedActivityItemsSortedSetKey, 0, -1);
        if (CollectionUtils.isEmpty(bytes)) {
            return Collections.emptyList();
        } else {
            return super.findByIds(bytes);
        }
    }

    /**
     * 获取指定活动下的所有活动项SortedSet Key
     *
     * @param activityId 活动ID
     * @return SortedSet Key
     */
    private String getActivityMappedActivityItemsSortedSetKey(Long activityId) {
        return super.getKeyByParams("act", activityId);
    }
}
